package com.v1.video.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.common.ToastAlone;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.BaseInfo;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RegistToRegistEntity;
import com.v1.video.domain.RegistVcodeEntity;
import com.v1.video.engine.NetEngine;
import com.v1.video.engine.RequestEngine;
import com.v1.video.exception.NetException;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;

import org.json.JSONException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用户注册
 * 
 * @author wangfang
 * 
 */
public class UserRegisterActivity extends BaseActivity implements
		OnClickListener {

	private static final long INTERVAL_TIME = 60000;
	private ProgressDialog mPd;
	private TextView mTvTitle, mTvAgreement;
	private EditText mEtPhonem, mEtVerify, mEtPwd, mEtComfirmPwd;
	private Button mBtnVerify, mBtnRegister;
	private Thread mTimerThread;
	private ImageView mIvPhoneDel, mIvPwdDel, mIvComfirmpwdDel;
	private boolean mIsExecute = false;
	private final int STATE_0 = 0x0001;
	private final int STATE_1 = 0x0002;
	private final int STATE_TO_LOGIN = 0x0003;
	private final int STATE_TOGO_JUMP = 0x0004;
	private final int STATE_GETVCODE_SUCCESS = 0x0005;
	private final int STATE_GETVCODE_FAIL = 0x0006;
	private final int STATE_REGIST_FAIL = 0x0007;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_register);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		mEtPhonem = (EditText) findViewById(R.id.et_phone);
		mEtVerify = (EditText) findViewById(R.id.et_verify);
		mEtPwd = (EditText) findViewById(R.id.et_pwd);
		mEtComfirmPwd = (EditText) findViewById(R.id.et_comfirmpwd);
		mBtnVerify = (Button) findViewById(R.id.btn_verify);
		mBtnRegister = (Button) findViewById(R.id.btn_register);
		mTvAgreement = (TextView) findViewById(R.id.tv_agreement);
//		mEtReqCode=(EditText)this.findViewById(R.id.et_reqcode);
		mIvPhoneDel = (ImageView) this.findViewById(R.id.iv_phone_del);
		mIvPwdDel = (ImageView) this.findViewById(R.id.iv_pwd_del);
		mIvComfirmpwdDel = (ImageView) this
				.findViewById(R.id.iv_comfirmpwd_del);
//		mIvReqcodeDel = (ImageView) this.findViewById(R.id.iv_reqcode_del);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mTvTitle.setText(R.string.userregister_register_text);
		String agreementStr = getString(R.string.setting_login_agreement);
		setAgreementText(agreementStr, 13, agreementStr.length());
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		findViewById(R.id.lay_result).setOnClickListener(this);
		mBtnVerify.setOnClickListener(this);
		mBtnRegister.setOnClickListener(this);
		this.findViewById(R.id.tv_login).setOnClickListener(this);
		mEtPhonem.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				String phoneNum = mEtPhonem.getText().toString();
				if (!TextUtils.isEmpty(phoneNum)) {
					mIvPhoneDel.setVisibility(View.VISIBLE);
				} else {
					mIvPhoneDel.setVisibility(View.GONE);
				}
				if (!TextUtils.isEmpty(phoneNum) && checkPhoneNum(phoneNum)) {
					setBtnVerifyState(true);
				} else {
					setBtnVerifyState(false);
				}
			}
		});

		mEtPwd.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					String pwd = mEtPwd.getText().toString();
					String comfirmpwd = mEtComfirmPwd.getText().toString();
					checkPassWord(pwd, comfirmpwd, true);
				}
			}
		});
		
		mEtPwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				String phoneNum = mEtPwd.getText().toString();
				if (!TextUtils.isEmpty(phoneNum)) {
					mIvPwdDel.setVisibility(View.VISIBLE);
				} else {
					mIvPwdDel.setVisibility(View.GONE);
				}
			}
		});

		mEtComfirmPwd.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					String pwd = mEtPwd.getText().toString();
					String comfirmpwd = mEtComfirmPwd.getText().toString();
					checkPassWord(pwd, comfirmpwd, false);
				}
			}
		});
		mEtComfirmPwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				String phoneNum = mEtComfirmPwd.getText().toString();
				if (!TextUtils.isEmpty(phoneNum)) {
					mIvComfirmpwdDel.setVisibility(View.VISIBLE);
				} else {
					mIvComfirmpwdDel.setVisibility(View.GONE);
				}
			}
		});
//		mEtReqCode.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before,
//					int count) {
//				// TODO Auto-generated method stub
//
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count,
//					int after) {
//				// TODO Auto-generated method stub
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//				// TODO Auto-generated method stub
//				String phoneNum = mEtReqCode.getText().toString();
//				if (!TextUtils.isEmpty(phoneNum)) {
//					mIvReqcodeDel.setVisibility(View.VISIBLE);
//				} else {
//					mIvReqcodeDel.setVisibility(View.GONE);
//				}
//			}
//		});
		mTvAgreement.setOnClickListener(this);
		mIvPhoneDel.setOnClickListener(this);
		mIvPwdDel.setOnClickListener(this);
		mIvComfirmpwdDel.setOnClickListener(this);
//		mIvReqcodeDel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.id.btn_verify:
			String phoneNum = mEtPhonem.getText().toString();
			if (checkMobile(phoneNum) == false) {
				return;
			}
			getVcode(phoneNum, 0);
			// getVcode(phoneNum);
			break;
		case R.id.btn_register:
			String mobile = mEtPhonem.getText().toString();
			if (checkMobile(mobile) == false) {
				return;
			}
			String mobilecode = mEtVerify.getText().toString();
			if (checkVerifyCode(mobilecode) == false) {
				return;
			}
			String pwd = mEtPwd.getText().toString();
			String comfirmpwd = mEtComfirmPwd.getText().toString();
			if (checkPassWord(pwd, comfirmpwd, true) == false) {
				return;
			}
			if (checkPassWord(pwd, comfirmpwd, false) == false) {
				return;
			}

			// userRegister(mobile, mobilecode, pwd);
			gotoRegist(mobile, mobilecode, pwd);
			break;
		case R.id.tv_agreement:
			Intent intent_protocol = new Intent(UserRegisterActivity.this,
					AboutUserProtocolActivity.class);
			startActivity(intent_protocol);
			break;
		case R.id.tv_login:
			this.finish();
			break;
		case R.id.iv_phone_del:
			mEtPhonem.setText("");
			mIvPhoneDel.setVisibility(View.GONE);
			break;
		case R.id.iv_pwd_del:
			mEtPwd.setText("");
			mIvPwdDel.setVisibility(View.GONE);
			break;
		case R.id.iv_comfirmpwd_del:
			mEtComfirmPwd.setText("");
			mIvComfirmpwdDel.setVisibility(View.GONE);
			break;
//		case R.id.iv_reqcode_del:
//			mEtReqCode.setText("");
//			mIvReqcodeDel.setVisibility(View.GONE);
//			break;
		default:
			break;
		}
	}

	private void setAgreementText(String str, int start, int end) {
		SpannableString sp = new SpannableString(str);
		sp.setSpan(
				new ForegroundColorSpan(getResources().getColor(
						R.color.userregister_agreement_text_color)), start,
				end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		mTvAgreement.setText(sp);
	}

	/**
	 * 验证密码
	 * 
	 * @param pwd
	 * @param comfirmpwd
	 * @param state
	 * @return
	 */
	private boolean checkPassWord(String pwd, String comfirmpwd, boolean state) {
		if (state) {
			if (!TextUtils.isEmpty(pwd)) {
				Pattern p2 = Pattern.compile("^[A-Za-z0-9]+$");
				Matcher m2 = p2.matcher(pwd);
				boolean b2 = m2.matches();
				if (pwd.length() < 6 || pwd.length() > 20 || !b2) {
					Toast.makeText(UserRegisterActivity.this, "密码格式错误",
							Toast.LENGTH_SHORT).show();
					return false;
				}
				return true;
			} else {
				Toast.makeText(UserRegisterActivity.this, "请输入密码",
						Toast.LENGTH_SHORT).show();
				return false;
			}
		} else {
			if (!TextUtils.isEmpty(comfirmpwd)) {
				if (!pwd.equals(comfirmpwd)) {
					Toast.makeText(UserRegisterActivity.this, "密码不一致",
							Toast.LENGTH_SHORT).show();
					return false;
				}
				Pattern p2 = Pattern.compile("^[A-Za-z0-9]+$");
				Matcher m2 = p2.matcher(comfirmpwd);
				boolean b2 = m2.matches();
				if (comfirmpwd.length() < 6 || comfirmpwd.length() > 20 || !b2) {
					Toast.makeText(UserRegisterActivity.this, "密码格式错误",
							Toast.LENGTH_SHORT).show();
					return false;
				}
				return true;
			} else {
				Toast.makeText(UserRegisterActivity.this, "请输入确认密码",
						Toast.LENGTH_SHORT).show();
				return false;
			}
		}
	}

	/**
	 * 验证码
	 * 
	 * @param code
	 * @return
	 */
	private boolean checkVerifyCode(String code) {
		if (TextUtils.isEmpty(code)) {
			Toast.makeText(UserRegisterActivity.this, "请输入验证码",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	/**
	 * 验证手机号
	 * 
	 * @param phoneNum
	 * @return
	 */
	private boolean checkMobile(String phoneNum) {
		if (!TextUtils.isEmpty(phoneNum)) {
			if (checkPhoneNum(phoneNum) == false) {
				Toast.makeText(UserRegisterActivity.this, "请输入正确的手机号",
						Toast.LENGTH_SHORT).show();
				return false;
			}
			return true;
		} else {
			Toast.makeText(UserRegisterActivity.this, "请输入手机号",
					Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	/**
	 * 手机号匹配
	 * 
	 * @param phoneNum
	 * @return
	 */
	private boolean checkPhoneNum(String phoneNum) {
		Pattern p = Pattern
				.compile("^((1[0-9][0-9])|(1[0-9][^4,\\D])|(1[0-9][0,5-9]))\\d{8}$");
		Matcher m = p.matcher(phoneNum);
		return m.matches();
	}

	private void getVcode(String phoneNum, int type) {
		MobclickAgent.onEvent(UserRegisterActivity.this, "register_id");
		String url = V1VideoHttpApi.GET_VCODE_FROM_REGISTER() + "&username=" + phoneNum
				+ "&devid=" + Constant.DEVICEID;
		Logger.i(TAG, "注册获取验证码Url =" + url);
		mPd = Utils.getProgressDialog(UserRegisterActivity.this, "正在加载数据……");
		mPd.show();
		RequestManager.getInstance().getRequest(UserRegisterActivity.this, url,
				null, RegistVcodeEntity.class, 0, "UserRegisterActivity",
				new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String key) {
						// TODO Auto-generated method stub
						Logger.i(TAG, "onSuccess.object=" + object.toString());
						Message msg = mHandler.obtainMessage();
						msg.what = STATE_GETVCODE_FAIL;
						mHandler.sendMessage(msg);
						if (object != null) {
							RegistVcodeEntity entity = (RegistVcodeEntity) object;
							com.v1.video.domain.BaseEntity.Header header = entity
									.getHeader();
							if (header == null) {
								ToastAlone.showToast(UserRegisterActivity.this,
										"未知错误", Toast.LENGTH_SHORT);
								return;
							} else if (!header.getStatus().equals("1")) {
								ToastAlone
										.showToast(UserRegisterActivity.this,
												header.getMessage(),
												Toast.LENGTH_SHORT);
								return;
							} else if (header.getStatus().equals("1")) {
								if (entity.getBody() != null) {
									if (entity.getBody().getData() != null) {
										if (entity.getBody().getData()
												.getStatus().equals("1")) {
											Message msgs = mHandler
													.obtainMessage();
											msgs.what = STATE_GETVCODE_SUCCESS;
											mHandler.sendMessage(msgs);
										}
									} else {

									}

								} else {
									// toast(header.getMessage());
								}
							}
						}

					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.i(TAG, message);
						Message msg = mHandler.obtainMessage();
						msg.what = STATE_GETVCODE_FAIL;
						mHandler.sendMessage(msg);

					}
				});
	}

	/**
	 * 获取验证码线程
	 * 
	 * @param phoneNum
	 */
	private void getVcode(final String phoneNum) {
		MobclickAgent.onEvent(UserRegisterActivity.this, "register_id");
		new AsyncTask<Void, Void, BaseInfo>() {

			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
				mPd = Utils.getProgressDialog(UserRegisterActivity.this, this);
				mPd.show();
			}

			@Override
			protected BaseInfo doInBackground(Void... params) {
				// TODO Auto-generated method stub
				BaseInfo baseInfo = null;
				try {
					baseInfo = new NetEngine().getVcode(phoneNum,
							Constant.GET_VCODE_REGIST + "");
				} catch (NetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				return baseInfo;

			}

			@Override
			protected void onPostExecute(BaseInfo result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				if (result != null && result.getCode() != null
						&& result.getCode().equals("1")) {
					resendThread();
				} else if (result != null) {
					Toast.makeText(UserRegisterActivity.this, result.getMsg(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(UserRegisterActivity.this, "验证码获取失败",
							Toast.LENGTH_SHORT).show();
				}

			}

		}.execute();

	}

	private void resendThread() {
		mIsExecute = true;
		setBtnVerifyState(false);
		mTimerThread = new Thread() {

			public void run() {

				long startTime = System.currentTimeMillis();

				while (mIsExecute
						&& (System.currentTimeMillis() - startTime < INTERVAL_TIME)) {

					Message msg = new Message();
					msg.what = 0;
					msg.arg1 = (int) ((INTERVAL_TIME
							- System.currentTimeMillis() + startTime) / 1000);
					mHandler.sendMessage(msg);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				;

				Message msg = new Message();
				msg.what = 1;
				mHandler.sendMessage(msg);
			}

		};
		mTimerThread.start();
	}

	private void setBtnVerifyState(boolean isState) {
		if (isState == true) {
			mBtnVerify.setText("获取验证码");
			mBtnVerify.setEnabled(true);
			mBtnVerify.setTextColor(this.getResources().getColor(R.color.userregister_item_verify_text_default_color));
			mBtnVerify.setBackgroundResource(R.drawable.btn_guanzhued);
		} else {
			mBtnVerify.setEnabled(false);
			mBtnVerify.setTextColor(this.getResources().getColor(R.color.userregister_item_verify_text_color));
			mBtnVerify.setBackgroundResource(R.drawable.btn_guanzhu_2);
		}

	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				mBtnVerify.setText(msg.arg1 + "S");
				break;
			case 1:
				closeTimer();
				setBtnVerifyState(true);
				break;
			case 2:
				userLoginNew();
				break;
			case STATE_TOGO_JUMP:
				gotoMainPage();
				break;
			case STATE_GETVCODE_SUCCESS:
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				resendThread();
				break;
			case STATE_GETVCODE_FAIL:
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				resendThread();
				break;
			case STATE_REGIST_FAIL:
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				if (msg.obj != null) {
					String message = (String) msg.obj;
					if (message != null && !message.equals("")) {
						ToastAlone.showToast(UserRegisterActivity.this, "未知错误",
								Toast.LENGTH_SHORT);
					}
				}

				break;

			}

		}

	};

	private void closeTimer() {
		mIsExecute = false;
		if (mTimerThread != null && mTimerThread.isAlive()) {
			mTimerThread.interrupt();
			mTimerThread = null;
		}
	}

	private void gotoRegist(final String mobile, final String mobilecode,
			final String pwd) {
		mHandler.sendEmptyMessage(1);
		mPd = Utils.getProgressDialog(UserRegisterActivity.this, "正在注册……");
		mPd.show();
		MobclickAgent.onEvent(UserRegisterActivity.this, "register_id");
		String url = V1VideoHttpApi.GET_INFO_TO_REGIST() + "&username=" + mobile
				+ "&password=" + pwd + "&code=" + mobilecode + "&devid="
				+ Constant.DEVICEID;
		Logger.i(TAG, "注册获取验证码Url =" + url);
		ParamList list = new ParamList();
		list.add(new Parameter("username", mobile));
		list.add(new Parameter("password", pwd));
		list.add(new Parameter("code", mobilecode));
		list.add(new Parameter("mac", Utils.getMacAddress(this)));
		RequestManager.getInstance().postRequest(UserRegisterActivity.this,
				url, list, RegistToRegistEntity.class, 0,
				"UserRegisterActivity", new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String key) {
						// TODO Auto-generated method stub
						Logger.i(TAG, "onSuccess.object=" + object.toString());

						if (object != null) {
							RegistToRegistEntity entity = (RegistToRegistEntity) object;
							com.v1.video.domain.BaseEntity.Header header = entity
									.getHeader();
							if (header == null) {
								ToastAlone.showToast(UserRegisterActivity.this,
										"未知错误", Toast.LENGTH_SHORT);
								Message msg = mHandler.obtainMessage();
								msg.what = STATE_REGIST_FAIL;
								mHandler.sendMessage(msg);
								return;
							} else if (!header.getStatus().equals("1")) {
								ToastAlone
										.showToast(UserRegisterActivity.this,
												header.getMessage(),
												Toast.LENGTH_SHORT);
								Message msg = mHandler.obtainMessage();
								msg.what = STATE_REGIST_FAIL;
								mHandler.sendMessage(msg);
								return;
							} else if (header.getStatus().equals("1")) {
								if (entity.getBody() != null) {
									if (entity.getBody().getStatus()
											.equals("1")) {
										Message msg = mHandler.obtainMessage();
										msg.what = 2;
										mHandler.sendMessage(msg);
									} else {
										Message msg = mHandler.obtainMessage();
										msg.what = STATE_REGIST_FAIL;
										msg.obj = entity.getBody().getMessage();
										mHandler.sendMessage(msg);
									}

								} else {
									// toast(header.getMessage());
									Message msg = mHandler.obtainMessage();
									msg.what = STATE_REGIST_FAIL;
									msg.obj = header.getMessage();
									mHandler.sendMessage(msg);
								}
							}
						}

					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.i(TAG, message);
						Message msg = mHandler.obtainMessage();
						msg.what = STATE_REGIST_FAIL;
						msg.obj = message;
						mHandler.sendMessage(msg);
					}
				});
	}

	/**
	 * 获取验证码线程
	 * 
	 * @param phoneNum
	 */
	private void userRegister(final String mobile, final String mobilecode,
			final String pwd) {
		mHandler.sendEmptyMessage(1);
		MobclickAgent.onEvent(UserRegisterActivity.this, "register_id");
		new AsyncTask<Void, Void, BaseInfo>() {

			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
				mPd = Utils.getProgressDialog(UserRegisterActivity.this,
						"正在注册……", this);
				mPd.show();
			}

			@Override
			protected BaseInfo doInBackground(Void... params) {
				// TODO Auto-generated method stub
				BaseInfo baseInfo = null;
				try {
					baseInfo = new NetEngine().userRegister(mobile, mobilecode,
							pwd);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return baseInfo;

			}

			@Override
			protected void onPostExecute(BaseInfo result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				if (result != null && !TextUtils.isEmpty(result.getCode())
						&& result.getCode().equals("1")) {
					userLogin();
				} else if (result != null
						&& !TextUtils.isEmpty(result.getMsg())) {
					Toast.makeText(UserRegisterActivity.this, result.getMsg(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(UserRegisterActivity.this, "注册失败",
							Toast.LENGTH_SHORT).show();
				}
			}

		}.execute();

	}

	private void userLoginNew() {
		RequestEngine request = new RequestEngine();
		request.getLoginInfo(UserRegisterActivity.this, mEtPhonem.getText()
				.toString(), mEtPwd.getText().toString(), mHandler,
				STATE_TOGO_JUMP, STATE_REGIST_FAIL);

	}

	private void gotoMainPage() {
		if (mPd != null && mPd.isShowing()) {
			mPd.dismiss();
		}
		mPd = null;
		Intent intent = new Intent(UserRegisterActivity.this,
				MainPageNewActivity.class);
		LoginInfo.getInstance().saveInstance(UserRegisterActivity.this);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("tag", MainPageNewActivity.FLAG_HEADLINE);
		startActivity(intent);
		UserRegisterActivity.this.finish();
	}

	private void userLogin() {
		new AsyncTask<Void, Void, LoginInfo>() {

			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
				mPd = Utils.getProgressDialog(UserRegisterActivity.this,
						"正在登录……", this);
				mPd.show();
			}

			@Override
			protected LoginInfo doInBackground(Void... params) {
				// TODO Auto-generated method stub
				LoginInfo loginInfo = null;
				try {
					loginInfo = new NetEngine().getLoginInfo(
							UserRegisterActivity.this, mEtPhonem.getText()
									.toString(), mEtPwd.getText().toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return loginInfo;

			}

			@Override
			protected void onPostExecute(LoginInfo result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				if (mPd != null && mPd.isShowing()) {
					mPd.dismiss();
				}
				mPd = null;
				if (result != null && result.getCode().equals("1")) {
					Intent intent = new Intent(UserRegisterActivity.this,
							MainPageNewActivity.class);
					result.saveInstance(UserRegisterActivity.this);
					LoginInfo.getInstance().saveInstance(
							UserRegisterActivity.this);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					intent.putExtra("tag", MainPageNewActivity.FLAG_HEADLINE);
					startActivity(intent);
					UserRegisterActivity.this.finish();
				} else if (result != null
						&& !TextUtils.isEmpty(result.getMsg())) {
					Toast.makeText(UserRegisterActivity.this, result.getMsg(),
							Toast.LENGTH_SHORT).show();
				} else {

				}
			}

		}.execute();
	}

}
